package com.example.understand_compose1.testcase_activity.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.example.understand_compose1.R

class MyAdapter(private val context: Context,
                private val titles: Array<String>,
                private val prices: Array<String>,
                private val pics: Array<Int>) : BaseAdapter() {

    override fun getCount(): Int {
        return titles.size
    }

    override fun getItem(position: Int): Any {
        return titles[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        // TODO: 这里还涉及到一个ListView优化问题，详细看B站比特猫编程
        // TODO: 我这里就不写了

        // 1. 加载item布局
        val view = LayoutInflater.from(context).inflate(R.layout.item_for_listview, parent, false)

        // 2. 获取控件
        val iv1 = view.findViewById<ImageView>(R.id.iv1)
        val tv1 = view.findViewById<TextView>(R.id.tv1) // 物品
        val tv2 = view.findViewById<TextView>(R.id.tv2) // 价格
        val tv3 = view.findViewById<TextView>(R.id.tv3) // 价格数字

        // 3. 设置数据
        iv1.setImageResource(pics[position])
        tv1.text = titles[position]
        tv3.text = prices[position]

        return view
    }
}